package ui;

import java.awt.*;
import java.util.Vector;

import javax.swing.*;

public class DevUi extends JFrame{
	JLabel a=new JLabel("设备A");
	JTextField jtf1=new JTextField("空闲中……");
	JLabel b1=new JLabel("设备B1");
	JTextField jtf2=new JTextField("空闲中……");
	JLabel b2=new JLabel("设备B1");
	JTextField jtf3=new JTextField("空闲中……");
	JLabel c1=new JLabel("设备C1");
	JTextField jtf4=new JTextField("空闲中……");
	JLabel c2=new JLabel("设备C2");
	JTextField jtf5=new JTextField("空闲中……");
	JLabel wait=new JLabel("等待队列");
	JList<Object> waitlist=new JList();
	public DevUi(){
		setLayout(null);
		JPanel jp1=new JPanel();
		jp1.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
		jp1.add(a);
		jp1.add(jtf1);
		jp1.add(b1);
		jp1.add(jtf2);
		JPanel jp2=new JPanel();
		jp2.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
		jp2.add(b2);
		jp2.add(jtf3);
		jp2.add(c1);
		jp2.add(jtf4);
		jp2.add(c2);
		jp2.add(jtf5);
		JPanel jp3=new JPanel();
		jp3.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
		
		jp3.add(wait);
		
	
		jp3.add(waitlist);
		jp1.setBounds(10, 10, 480, 100);
		jp2.setBounds(10, 110, 480, 100);
		jp3.setBounds(10,210,480,100);
		this.add(jp1);
		this.add(jp2);
		this.add(jp3);
		
		this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
		this.setTitle("设备管理器");
		this.setSize(600,400);
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		//this.setVisible(true);
		
		T t=new T();
		t.setDaemon(true);
		t.start();
		
	}
	
	private void  w(){
		java.util.Set<Integer> waitQueue=controller.Dev.getObject().getWaitQueue();
		Vector<Object> keys = null;
		keys=new Vector<Object>();
		for(Integer key:waitQueue){
			keys.add("进程"+key);
		}
		waitlist.setListData(keys);
	}
	
	public void setUi(){
		java.util.Map<String,Integer> map=controller.Dev.getObject().getProcAndResource();
		java.util.Set<String> keys=map.keySet();
		for(String key:keys){
			Integer procId=map.get(key);
			
			switch(key){
			case"A":jtf1.setText("进程"+procId+"在使用中");break;
			case "B1":jtf2.setText("进程"+procId+"在使用中");break;
			case "B2":jtf3.setText("进程"+procId+"在使用中");break;
			case "C1":jtf4.setText("进程"+procId+"在使用中");break;
			case "C2":jtf5.setText("进程"+procId+"在使用中");break;
			default:System.out.println(procId+"空闲中");break;
			}
		
		}
	}
	
	public void start(){
		this.setVisible(true);
	}
	
	private class T extends Thread{
		public void run(){
			
			try{
				while(true){
				setUi();
				w();
				Thread.sleep(500);
				}
			}catch(Exception error){}
		}
	}
	
}
